<template>
  <div class="ai-search">
    <div class="search-container">
      <img src="@/assets/icon-search.png" alt="Icon" class="search-icon" />
      <input v-model="query" type="text" placeholder="搜索课程" />
      <button @click="search">搜索</button>
    </div>
    <transition name="fade">
      <div class="search-results" v-if="results.length > 0">
        <p>共找到相关结果 {{ results.length }} 个</p>
        <div v-for="result in formattedResults" :key="result.id" class="result-item">
          <h3>{{ result.title }}</h3>
          <div class="left">
            <p>{{ result.courseName }}</p>
            <p>{{ result.schoolName }}</p>
          </div>
          <div class="right">
            <p>案例： {{ result.caseCount }}</p>
            <p>学习人数： {{ result.membersCount }}</p>
          </div>
        </div>
        <el-pagination
          size="small"
          background
          layout="prev, pager, next"
          :total="50"
          class="mt-4"
        />
      </div>
    </transition>
    <transition name="fade">
      <div class="no-results" v-if="results.length === 0 && query">
        <p>未找到相关结果。</p>
      </div>
    </transition>
  </div>
</template>
    
  <script>
  import axios from 'axios';
  export default {
  name: "HomeSearch",
  data() {
    return {
      query: "",
      results: [],
      formattedResults: [],
    };
  },
  created() {
    this.fetchResults(this.$route.query.query);  
  },
  watch: {  
    // 监听路由查询参数的变化，并在变化时重新获取数据  
    '$route.query.query'(newVal) {  
      if (newVal) {  
        this.query = newVal; // 可选：更新组件内部的查询字符串  
        this.fetchResults(newVal);  
      }  
    }  
  },  
  methods: {
    search(newQuery) {  
      // 如果search方法被传递了一个新查询，则使用它；否则，使用组件内部的查询字符串  
      const query = newQuery || this.query;  
      this.query = query; // 更新组件内部的查询字符串  
      this.fetchResults(query); // 调用fetchResults进行搜索  
    },
    async fetchResults(query = '') {    
      try {    
          
        const response = await axios.get(`/api/search?query=${encodeURIComponent(query)}`); //写地址   
        this.results = response.data;    
        // 假设response.data是一个包含多个对象的数组  
        this.formattedResults = this.results.map(item => ({    
          id: item.id,    
          schoolName: item.schoolName,    
          courseName: item.courseName,   
          caseCount: item.caseCount,    
          membersCount: item.membersCount,    
        }));    
      } catch (error) {    
        console.error('Error fetching results:', error);    
        this.results = [];   
        this.formattedResults = []; 
      }    
    }    
  }  
};
</script>

<style scoped>
.ai-search {
  padding: 20px;
  background: rgba(250, 250, 250, 1);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.search-container {
  display: flex;
  width: 30%;
  margin-bottom: 20px;
  position: relative;
  border: 1px solid rgba(42, 130, 228, 1); /* 为整个容器添加边框，可选 */
  border-radius: 8px;
}

.search-icon {
  position: absolute;
  left: 10px;
  width: 26px;
  height: 26px;
  top: 50%;
  transform: translateY(-50%); /* 垂直居中图标 */
}

.search-container input {
  flex: 1; /* 输入框占据大部分空间 */
  padding: 10px 0 10px 40px; /* 调整padding以适应图标 */
  border: none; /* 移除输入框的边框，因为容器已经有边框了 */
  border-radius: 8px 0 0 8px; /* 左侧圆角 */
  outline: none;
}

.search-container button {
  flex: 0 0 auto;
  padding: 6px 15px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 8px; /* 右侧圆角 */
  cursor: pointer;
  transition: background-color 0.3s;
  margin: 2px 2px 2px 2px;
}

.search-container button:hover {
  background-color: #0056b3;
}

.search-results {
  width: 70%;
  background-color: rgba(255, 255, 255, 1);
  border-radius: 8px;
  box-shadow: 0px 2px 10px rgba(204, 204, 204, 0.25);
  animation: fadeIn 0.5s ease-in-out;
  text-align: left;
}
.search-results p {
  margin: 15px 0 0 25px;
  font-size: 14px;
}
.result-item {
  padding: 25px;
  border-bottom: 1px solid rgba(128, 128, 128, 1);
  transition: transform 0.3s;
}

.result-item h3 {
  margin: 0;
  font-size: 18px;
  color: rgba(59, 150, 255, 1);
  text-align: left;
  padding: 15px 0 30px 20px;
  font-size: 18px;
}
.left {
  display: inline-block;
}
.right {
  display: inline-block;
  margin: 0;
  margin-left: 540px;
}
.left p {
  margin: 5px 0;
  color: rgba(102, 102, 102, 1);
  display: inline;
  padding: 20px 0 20px 20px;
}
.right p {
  margin: 5px 0;
  color: rgba(102, 102, 102, 1);
  display: inline;
  padding: 20px 0 20px 20px;
}

.result-item:hover {
  transform: translateY(-5px);
}

.no-results {
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  text-align: center;
  color: #888;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s;
}

.fade-enter, .fade-leave-to  {
  opacity: 0;
}
.pagination{
  padding: 60px 0 60px 0;
 margin-left: 281px;
  
}
</style>
